__author__ = 'zoulida'

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN

def product():

    from sklearn import datasets
    #matplotlib inline
    X1, y1 = datasets.make_circles(n_samples=5000, factor=.6,
                                 noise=.05)
    X2, y2 = datasets.make_blobs(n_samples=1000, n_features=3, centers=[[1.2,1.2]], cluster_std=[[.1]],
                                 random_state=9)
    X3, y3 = datasets.make_moons(n_samples=1000, shuffle=True, noise=0.05, random_state=56)

    X4,y_true = datasets.make_blobs(n_samples = 30000,   # 生成300条数据
                          centers = [[1.2,1.2],[3.2,8.2],[0.2,8.2],[8.2,4.2],[9.2,9.2],[4.2,2.2],[5.2,-2.2],[7.2,1.2],[2,5],[6,6]],        # 四类数据
                          cluster_std = [0.15,0.4,0.05,0.15,0.5,0.3,0.4,0.6,0.08,0.2],  #[0.9,1.0,1,0.8],  # 方差一致
                          random_state = 2,
                          n_features=2)

    X = np.concatenate((X4,  ))
    #plt.scatter(X[:, 0], X[:, 1], marker='o')#, c=y_true)
    plt.scatter(X[:, 0], X[:, 1], marker='o', c=y_true)
    plt.show()
    return X


def dbscan(X):
    y_pred = DBSCAN(eps = 0.2, min_samples = 10).fit_predict(X)
    plt.scatter(X[:, 0], X[:, 1], c=y_pred)
    plt.show()

def kmeans(X):
    from sklearn.cluster import KMeans
    y_pred = KMeans(n_clusters=3, random_state=9).fit_predict(X)
    plt.scatter(X[:, 0], X[:, 1], c=y_pred)
    plt.show()

X = product()


import time
start_time = time.time()
dbscan(X)
elapse_time = time.time() - start_time
print(elapse_time)


start_time = time.time()
#kmeans(X)
elapse_time = time.time() - start_time
print(elapse_time)


